//匿名函数
//可以避免变量污染全局作用域
var addUserModel;
var updateUserModel;
(function () {
    var form = layui.form;
    form.verify({
        checkUserName: function (value) {
            if (value.length < 5) {
                return "用户名不能小于5位";
            }
        },
        checkPassword: function (value) {
            if (value.length < 5) {
                return "密码不能小于5位";
            }
        }
    });

    form.on('submit(addUserBtn)', function (data) {
        var $ = layui.$;
        var userName = data.field.userName;
        var userPwd = data.field.userPwd;
        $.ajax({
            url: "../user/insert",
            method: "post",
            data: {
                "userName": userName,
                "userPwd": userPwd
            },
            success: function (data) {
                console.log(data)
                if (data.code == 200) {

                    layui.layer.close(addUserModel);
                    layui.table.reload("demo");

                } else if (data.code == 500) {
                    var layer = layui.layer;
                    layer.msg(data.msg, {icon: 2});
                    setTimeout(function () {
                    },1000)
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
        return false;
    });


    form.on('submit(updateUserBtn)', function (data) {

        var userId = layui.$("#editUserForm input[name='userId']").val();
        var userName = data.field.userName;
        var userPwd = data.field.userPwd;
        layui.$.ajax({
            url: "../user/updateUser",
            method: "put",
            data: {
                "userId":userId,
                "userName": userName,
                "userPwd": userPwd
            },
            success: function (data) {
                console.log(data)
                if (data.code == 200) {
                    layui.layer.close(updateUserModel);
                    layui.table.reload("demo");

                } else if (data.code == 500) {
                    layui.layer.msg(data.msg, {icon: 2});
                    setTimeout(function () {
                    },1000)
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
        return false;
    });
})();
(function(){
    var table= layui.table;
    table.render({
        elem: '#demo'
        ,url: '../user/findByPage' //数据接口
        ,height: 500
        ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left'}
            ,{field: 'userId', title: '用户Id', width:80, sort: true, fixed: 'left'}
            ,{field: 'userName', title: '用户名', width:100}
            ,{field: 'userPwd', title: '密码', width:100, sort: true}
            ,{field: 'createTime', title: '创建时间', width:250}
            ,{field: 'updateTime', title: '更新时间', width: 250}
        ]]
        ,limit: 10
        ,page: true
        ,response: {
            statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
        }
        ,parseData: function(res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },where:{
            "userName":layui.$("input[name='searchUsername']").val()
        }
    });
})();
//验证信息提示
function qxMsg(msgStr) {
    var layer = layui.layer;
    layer.open({
        type: 1,
        title: '提示消息',
        offset: '10%;',
        content: '<div style="padding: 20px 80px;">'+msgStr+'</div>',
        btn: '关闭',
        btnAlign: 'c',
        shade: 0.2,
        yes: function(index, layero) {
            layer.close(index);
        }
    });
}

function clearForm() {
    var $ = layui.$;
    $("input[name='userName'] ").val("");
    $("input[name='userPwd'] ").val("");
}

function editUser() {
//获取被选中项
    var checkStatus = layui.table.checkStatus('demo');
    if (checkStatus.data.length ==0){
        layui.layer.msg("请选择数据",{icon:2})
        setTimeout(function () {
        },1000)
        return;
    }
    if(checkStatus.data.length >1){
        layui.layer.msg("请选择一条数据",{icon:2});
        setTimeout(function () {
        },1000)
        return;
    }
    var userId = checkStatus.data[0].userId;
    //从后台加载数据
    layui.$.ajax({
        url:"../user/user/"+userId,
        method:"get",
        success:function (result) {
            console.log(result.data);
            //填充到编辑框中
            layui.$("#editUserModel input[name='userId']").val(result.data.userId);
            layui.$("#editUserModel input[name='userName']").val(result.data.userName);
            layui.$("#editUserModel input[name='userPwd']").val(result.data.userPwd);

            //展示编辑框
            updateUserModel = layui.layer.open({
                type: 1,
                title: "编辑用户信息",
                content: layui.$("#editUserModel")
            });
            //点击确定提交

        }
    });
}
function showAddModel() {
    clearForm();
    var layer = layui.layer;
    var $ = layui.$;
    addUserModel = layer.open({
        type:1,
        title:"添加用户信息",
        content:$("#addUserModel"),
    });
}
function deleteUser(){
    //获取被选中项
    //通过Ajax 发起异步删除请求
    var checkStatus = layui.table.checkStatus('demo'); //idTest 即为基础参数 id 对应的值
    if(checkStatus.data.length==0){
        layui.layer.msg("请选择数据",{icon:2});
        setTimeout(function () {
        },1000)
        return;
    }
    console.log(checkStatus.data) //获取选中行的数据
    var userIds=new Array();
    for (let i=0 ;i<checkStatus.data.length; i++ ){
        userIds.push(checkStatus.data[i].userId);
    }
    layui.$.ajax({
        url:"../user/batchDelete?userIds="+userIds.join(),
        method: "delete",
        success:function (data) {
            layui.layer.msg(data.msg,{icon:1});
            setTimeout(function () {
            },1000)
            layui.table.reload("demo");
        },error: function(data){
        }
    })
}
function reloadTable() {
    var table = layui.table;
    table.reload("demo");
}
function searchUser(){
    layui.table.reload("demo",{
        where:{
            "userName":layui.$("input[name='searchUsername']").val()
        }
    });
}